package com.sunfeng.elasticsearch.document.search;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

/**
 * @Author : 清风冷影
 * @Description:
 * @Date : 2021/11/4  21:00
 */
@SpringBootTest
public class ESGoods_Doc_Query_SpringBoot {

    @Test
    void contextLoads() throws IOException {
        // 创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.139.187", 9200, "http"))
        );
        // searchAll(client);
        termQuery(client);
        client.close();
    }


    private void termQuery(RestHighLevelClient client) throws IOException {
        SearchRequest request = new SearchRequest();
        request.indices("goods");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.termQuery("price", 4999));

        request.source(sourceBuilder);

        SearchResponse search = client.search(request, RequestOptions.DEFAULT);

        SearchHits hits = search.getHits();

        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }

    }

    private void rangeQuery(RestHighLevelClient client) throws IOException {
        SearchRequest request = new SearchRequest();
        request.indices("goods");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("price");
        //小于等于
        rangeQueryBuilder.gte(1000);
        //小于等于
        rangeQueryBuilder.lte(3000);
        sourceBuilder.query(rangeQueryBuilder);
        request.source(sourceBuilder);
        SearchResponse search = client.search(request, RequestOptions.DEFAULT);

        SearchHits hits = search.getHits();

        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }
    }

    private void booleQuery(RestHighLevelClient client) throws IOException {
        SearchRequest request = new SearchRequest();
        request.indices("goods");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("price");

        rangeQuery.gte(1000);
        rangeQuery.lte(3000);

        boolQuery.must(rangeQuery);
        boolQuery.must(QueryBuilders.matchPhraseQuery("title", "超米"));

        sourceBuilder.query(boolQuery);
        request.source(sourceBuilder);
        SearchResponse search = client.search(request, RequestOptions.DEFAULT);

        SearchHits hits = search.getHits();

        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }
    }

    private void filterQuery(RestHighLevelClient client) throws IOException {
        SearchRequest request = new SearchRequest();
        request.indices("goods");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "小米手机");

//        rangeQuery.gte(1000);
//        rangeQuery.lte(3000);
//
//        boolQuery.filter(QueryBuilders.rangeQuery())
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(2799).lte(3000));
        boolQueryBuilder.must(queryBuilder);

        sourceBuilder.query(boolQueryBuilder);
        request.source(sourceBuilder);
        SearchResponse search = client.search(request, RequestOptions.DEFAULT);

        SearchHits hits = search.getHits();

        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }
    }

    private void sortQuery(RestHighLevelClient client) throws IOException {
        SearchRequest request = new SearchRequest();
        request.indices("goods");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "小米手机");


        sourceBuilder.sort("price", SortOrder.DESC);

        sourceBuilder.query(queryBuilder);
        request.source(sourceBuilder);
        SearchResponse search = client.search(request, RequestOptions.DEFAULT);

        SearchHits hits = search.getHits();

        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }
    }

    private void pageQuery(RestHighLevelClient client) throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("goods");

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.from(0);
        sourceBuilder.size(3);

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        boolQueryBuilder.must(QueryBuilders.matchQuery("title", "小米手机"));
        sourceBuilder.query(boolQueryBuilder);
        searchRequest.source(sourceBuilder);

        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);

        SearchHits hits = search.getHits();

        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }
    }

    private void highlightQuery(RestHighLevelClient client) throws IOException {
        SearchRequest request = new SearchRequest();
        request.indices("goods");

        SearchSourceBuilder builder = new SearchSourceBuilder();
        MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("title", "小米");
        HighlightBuilder highlightBuilder = new HighlightBuilder();

        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        highlightBuilder.field("title");
        builder.highlighter(highlightBuilder);

        builder.query(matchQuery);
        request.source(builder);

        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);


        SearchHits hits = searchResponse.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(searchResponse.getTook());
        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }
    }

    private void _sourceQuery(RestHighLevelClient client) throws IOException {
        SearchRequest request = new SearchRequest();
        request.indices("goods");

        SearchSourceBuilder builder = new SearchSourceBuilder();

        MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("title", "小米");
        String[] include = {};
        String[] excludes = {"images"};
        builder.fetchSource(include,excludes);
        builder.query(matchQuery);
        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);


        SearchHits hits = searchResponse.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(searchResponse.getTook());
        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }
    }
}
